﻿using System;
using System.Linq;
using LayerData.DataBaseManager;
using LayerEntity.Hotel;
using Npgsql;
using System.Data;
namespace LayerData.Hotel
{
    public class DHabitacion
    {
        #region Variables
        public IPostgreSqlConnectionInfo CurrentConection { get; set; }
        #endregion

        #region Constructor
        public DHabitacion(IPostgreSqlConnectionInfo con)
        {

            this.CurrentConection = con;
        }

        #endregion
        #region Funciones
        public string MantenimientoHabitaciones(EHabitacion habitacion, ETarifaHotel Etarifa, EnumOperaciones operacion)
        {
            String proc = "";
            String Msg = "";
            NpgsqlConnection con = null;
            Console.Write("Id pido Hotel :" + habitacion.Id_pisohotel + "\n");
            bool res = false;
            try
            {
                con = this.CurrentConection.GetPostgreSqlConnection();
                NpgsqlTransaction tran = this.CurrentConection.GetTransaction(con);
                proc = (string)PostgreSqlHelper.ExecuteScalar(this.CurrentConection, tran, "hotel.isp_grabar_habitacion", habitacion.ToObject((int)operacion));
                String idHabita = string.Empty;
                if ((int)operacion == 1)
                {
                    idHabita = proc.Substring(1, proc.Length-1);
                    Msg = proc.Substring(0, 1);
                }
                else
                {
                    Msg = proc;
                    idHabita = habitacion.Id_habitacion.ToString();
                }
                if (Msg.Substring(0, 1) == "0")
                {
                    object[] Dataobj = new object[]
                    {
                        int.Parse(idHabita)
                    };
                    proc = (string)PostgreSqlHelper.ExecuteScalar(this.CurrentConection, tran, "HOTEL.ISP_GRBA_TARIFA_HOTEL", Etarifa.ToObject((int)operacion, Dataobj));
                    if (proc == "0")
                    {
                        res = true;
                    }
                    else
                    {
                        if (proc == "-11")
                        {
                            res = false;
                            this.CurrentConection.CancelarConexionTrans(CurrentConection.GetPostgreSqlConnection());

                        }
                        else
                        {
                            if (proc == "-20")
                            {
                                res = false;
                                this.CurrentConection.CancelarConexionTrans(CurrentConection.GetPostgreSqlConnection());

                            }
                            else
                            {
                                if (proc == "-12")
                                {
                                    res = false;
                                    this.CurrentConection.CancelarConexionTrans(CurrentConection.GetPostgreSqlConnection());

                                }
                                else
                                {
                                    if (proc == "-22")
                                    {
                                        res = false;
                                        this.CurrentConection.CancelarConexionTrans(CurrentConection.GetPostgreSqlConnection());

                                    }
                                    else
                                    {
                                        res = false;
                                        this.CurrentConection.CancelarConexionTrans(CurrentConection.GetPostgreSqlConnection());
                                    }

                                }
                            }

                        }

                    }
                }
                else
                {
                    if (Msg == "-1")
                    {
                        res = false;
                        this.CurrentConection.CancelarConexionTrans(CurrentConection.GetPostgreSqlConnection());
                    }
                    else
                    {
                        if (Msg == "-10")
                        {
                            res = false;
                            this.CurrentConection.CancelarConexionTrans(CurrentConection.GetPostgreSqlConnection());
                        }
                        else
                        {
                            if (Msg == "-2")
                            {
                                res = false;
                                this.CurrentConection.CancelarConexionTrans(CurrentConection.GetPostgreSqlConnection());
                            }
                            else
                            {
                                if (Msg == "-30")
                                {
                                    res = false;
                                    this.CurrentConection.CancelarConexionTrans(CurrentConection.GetPostgreSqlConnection());
                                }
                                else
                                {
                                    res = false;
                                    this.CurrentConection.CancelarConexionTrans(CurrentConection.GetPostgreSqlConnection());
                                }

                            }
                        }
                    }


                }
                if (res == true)
                {

                    this.CurrentConection.CommintTransaction(tran);
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                this.CurrentConection.CancelarConexionTrans(CurrentConection.GetPostgreSqlConnection());
                proc = ex.Message;
                throw;

            }
            finally
            {
                this.CurrentConection.CloseConnection(con);
            }
            return proc;
        }
        public string MantenimientoTarifas(ETarifaHotel Etarifa, EnumOperaciones operacion)
        {
            String proc = "";
            bool res = false;
            NpgsqlConnection con = null;
            try
            {
                object[] Dataobj = new object[]
                    {
                        Etarifa.id_habitacion
                    };
                con = this.CurrentConection.GetPostgreSqlConnection();
                NpgsqlTransaction tran = this.CurrentConection.GetTransaction(con);
                proc = (string)PostgreSqlHelper.ExecuteScalar(this.CurrentConection, tran, "HOTEL.ISP_GRBA_TARIFA_HOTEL", Etarifa.ToObject((int)operacion, Dataobj));
                if (proc == "0")
                {
                    res = true;
                }
                else
                {
                    if (proc == "-11")
                    {
                        res = false;
                        this.CurrentConection.CancelarConexionTrans(CurrentConection.GetPostgreSqlConnection());

                    }
                    else
                    {
                        if (proc == "-20")
                        {
                            res = false;
                            this.CurrentConection.CancelarConexionTrans(CurrentConection.GetPostgreSqlConnection());

                        }
                        else
                        {
                            if (proc == "-12")
                            {
                                res = false;
                                this.CurrentConection.CancelarConexionTrans(CurrentConection.GetPostgreSqlConnection());

                            }
                            else
                            {
                                if (proc == "-22")
                                {
                                    res = false;
                                    this.CurrentConection.CancelarConexionTrans(CurrentConection.GetPostgreSqlConnection());

                                }
                                else
                                {
                                    res = false;
                                    this.CurrentConection.CancelarConexionTrans(CurrentConection.GetPostgreSqlConnection());
                                }

                            }
                        }

                    }

                }
                if (res == true)
                {

                    this.CurrentConection.CommintTransaction(tran);
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                this.CurrentConection.CancelarConexionTrans(CurrentConection.GetPostgreSqlConnection());
                proc = ex.Message;
                throw;

            }
            finally
            {
                this.CurrentConection.CloseConnection(con);
            }
            return proc;
        }
        public DataTable getListadoHabitaciones(EHabitacion obj)
        {
            DataTable dtb;
            NpgsqlConnection con = null;
            NpgsqlDataAdapter da = null;
            try
            {
                con = this.CurrentConection.GetPostgreSqlConnection();
                da = new NpgsqlDataAdapter("select *from hotel.v_listar_habitacion where num_habitacion || tipo_habitacion ilike '%" + obj.Num_habitacion + "%'", con);
                dtb = new DataTable();
                da.Fill(dtb);
            }
            catch (NpgsqlException ex)
            {
                CurrentConection.CloseConnection(con);
                Console.Write(ex.Message + "/" + ex.Source);
                return null;
            }
            finally
            {
                CurrentConection.CloseConnection(con);
            }
            return dtb;
        }
        public DataTable getListadoTarifas(EHabitacion obj)
        {
            DataTable dtb;
            NpgsqlConnection con = null;
            NpgsqlDataAdapter da = null;
            try
            {
                con = this.CurrentConection.GetPostgreSqlConnection();
                da = new NpgsqlDataAdapter("select *from hotel.VLISTAR_TARIFAS where id_habitacion=" + obj.Id_habitacion + " ", con);
                dtb = new DataTable();
                da.Fill(dtb);
            }
            catch (NpgsqlException ex)
            {
                CurrentConection.CloseConnection(con);
                Console.Write(ex.Message + "/" + ex.Source);
                return null;
            }
            finally
            {
                CurrentConection.CloseConnection(con);
            }
            return dtb;
        }
        #endregion
    }
}